package ar.republicagroup.gestion.view;

import ar.republicagroup.gestion.entities.Cliente;
import ar.republicagroup.gestion.services.ClienteServices;
import ar.republicagroup.gestion.util.SpringUtils;
import ar.republicagroup.gestion.util.Validar;
import ar.republicagroup.gestion.util.messages.UIMessages;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class modalPanelAddCliente extends javax.swing.JDialog {
    
    ClienteServices clienteServices = (ClienteServices)SpringUtils.getBean("clienteServices");
   
    
    
    //Cliente mi_cliente = new Cliente();
    
    private static final int ULTIMO = 1;
    private static final int NO_ULTIMO = 0;
    
    List<JTextField> txtList = new ArrayList<>();
    List<Map<String,Object>> mapita = new ArrayList<>();
    
    
    public modalPanelAddCliente(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        cargarListaDeJTextFieldObligatorios();
        valoresPorDefecto();
        
        txtCliente.requestFocus();
    }

   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelContenedor = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtTelefono = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtDireccion = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtCelular = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtComBsAs = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtComInt = new javax.swing.JTextField();
        btnRegistrar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        lblErrorCliente = new javax.swing.JLabel();
        lblErrorTelefono = new javax.swing.JLabel();
        lblErrorDireccion = new javax.swing.JLabel();
        lblErrorCelular = new javax.swing.JLabel();
        lblErrorComBsAs = new javax.swing.JLabel();
        lblErrorComInt = new javax.swing.JLabel();
        lblErrorEmail = new javax.swing.JLabel();
        txtNextel = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        chkComisionRechazo = new javax.swing.JCheckBox();
        txtCliente = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtComDepo = new javax.swing.JTextField();
        lblErrorComDepo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("...: Alta de cliente :...");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        panelContenedor.setBorder(javax.swing.BorderFactory.createTitledBorder("Nuevo cliente"));
        panelContenedor.setPreferredSize(new java.awt.Dimension(530, 301));

        jLabel1.setText("Cliente");

        jLabel2.setText("Teléfono");

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("ar/republicagroup/gestion/view/properties/modalPanelAddCliente_es"); // NOI18N
        txtTelefono.setName(bundle.getString("txtTelefono")); // NOI18N
        txtTelefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtTelefonoKeyPressed(evt);
            }
        });

        jLabel3.setText("Dirección");

        txtDireccion.setName(bundle.getString("txtDireccion")); // NOI18N
        txtDireccion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtDireccionKeyPressed(evt);
            }
        });

        jLabel4.setText("Email");

        txtEmail.setName(bundle.getString("txtEmail")); // NOI18N
        txtEmail.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtEmailKeyPressed(evt);
            }
        });

        jLabel5.setText("Celular");

        txtCelular.setName(bundle.getString("txtCelular")); // NOI18N
        txtCelular.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCelularKeyPressed(evt);
            }
        });

        jLabel6.setText("Com.CAP");

        txtComBsAs.setText("10.00");
        txtComBsAs.setName(bundle.getString("txtComBsAs")); // NOI18N
        txtComBsAs.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtComBsAsKeyPressed(evt);
            }
        });

        jLabel7.setText("Com.VC");

        txtComInt.setText("10.00");
        txtComInt.setName(bundle.getString("txtComInt")); // NOI18N
        txtComInt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtComIntKeyPressed(evt);
            }
        });

        btnRegistrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/add.png"))); // NOI18N
        btnRegistrar.setText("Registrar");
        btnRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistrarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/cancel.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        lblErrorCliente.setForeground(new java.awt.Color(255, 0, 0));
        java.util.ResourceBundle bundle1 = java.util.ResourceBundle.getBundle("ar/republicagroup/gestion/util/messages/gestionAppErrorMessages"); // NOI18N
        lblErrorCliente.setText(bundle1.getString("requiredField")); // NOI18N

        lblErrorTelefono.setForeground(new java.awt.Color(255, 0, 0));
        lblErrorTelefono.setText(bundle1.getString("requiredField")); // NOI18N

        lblErrorDireccion.setForeground(new java.awt.Color(255, 0, 0));
        lblErrorDireccion.setText(bundle1.getString("requiredField")); // NOI18N

        lblErrorCelular.setForeground(new java.awt.Color(255, 0, 0));
        lblErrorCelular.setText(bundle1.getString("requiredField")); // NOI18N

        lblErrorComBsAs.setForeground(new java.awt.Color(255, 0, 0));
        lblErrorComBsAs.setText(bundle1.getString("requiredField")); // NOI18N

        lblErrorComInt.setForeground(new java.awt.Color(255, 0, 0));
        lblErrorComInt.setText(bundle1.getString("requiredField")); // NOI18N

        lblErrorEmail.setForeground(new java.awt.Color(255, 0, 0));
        lblErrorEmail.setText(bundle1.getString("email")); // NOI18N

        txtNextel.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtNextelKeyPressed(evt);
            }
        });

        jLabel8.setText("Nextel");

        chkComisionRechazo.setSelected(true);
        chkComisionRechazo.setText("Comisión de rechazo");

        txtCliente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtClienteKeyPressed(evt);
            }
        });

        jLabel9.setText("Com. Depósito");

        txtComDepo.setText("0.00");
        txtComDepo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtComDepoKeyPressed(evt);
            }
        });

        lblErrorComDepo.setForeground(new java.awt.Color(255, 0, 0));
        lblErrorComDepo.setText(bundle1.getString("requiredField")); // NOI18N

        javax.swing.GroupLayout panelContenedorLayout = new javax.swing.GroupLayout(panelContenedor);
        panelContenedor.setLayout(panelContenedorLayout);
        panelContenedorLayout.setHorizontalGroup(
            panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelContenedorLayout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelContenedorLayout.createSequentialGroup()
                        .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNextel, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                            .addComponent(txtDireccion)
                            .addComponent(txtTelefono)
                            .addComponent(txtCelular)
                            .addComponent(txtEmail)
                            .addComponent(txtCliente))
                        .addGap(18, 18, 18)
                        .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblErrorCelular)
                            .addComponent(lblErrorEmail)
                            .addComponent(lblErrorDireccion)
                            .addComponent(lblErrorTelefono)
                            .addComponent(lblErrorCliente))
                        .addGap(119, 119, 119))
                    .addGroup(panelContenedorLayout.createSequentialGroup()
                        .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chkComisionRechazo)
                            .addGroup(panelContenedorLayout.createSequentialGroup()
                                .addComponent(btnRegistrar)
                                .addGap(18, 18, 18)
                                .addComponent(btnCancelar))
                            .addGroup(panelContenedorLayout.createSequentialGroup()
                                .addComponent(txtComBsAs, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lblErrorComBsAs)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(panelContenedorLayout.createSequentialGroup()
                        .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtComDepo, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtComInt, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblErrorComInt)
                            .addComponent(lblErrorComDepo))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        panelContenedorLayout.setVerticalGroup(
            panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelContenedorLayout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblErrorCliente)
                    .addComponent(txtCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblErrorTelefono))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblErrorDireccion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(lblErrorCelular))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNextel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblErrorEmail)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtComBsAs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(lblErrorComBsAs))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtComInt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblErrorComInt)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtComDepo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblErrorComDepo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addComponent(chkComisionRechazo)
                .addGap(18, 18, 18)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCancelar)
                    .addComponent(btnRegistrar))
                .addGap(28, 28, 28))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelContenedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelContenedor, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtTelefonoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTelefonoKeyPressed
       if(evt.getKeyCode()==evt.VK_ENTER){
           validarSiCajaDeTextoEsVacia(txtTelefono.getText(), txtTelefono, lblErrorTelefono, txtDireccion,NO_ULTIMO);
       }
    }//GEN-LAST:event_txtTelefonoKeyPressed

    private void txtDireccionKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDireccionKeyPressed
        if(evt.getKeyCode()==evt.VK_ENTER){
           validarSiCajaDeTextoEsVacia(txtDireccion.getText(), txtDireccion, lblErrorDireccion, txtCelular,NO_ULTIMO);
       }
    }//GEN-LAST:event_txtDireccionKeyPressed

    private void txtEmailKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtEmailKeyPressed
         if(evt.getKeyCode()==evt.VK_ENTER){
           if(!Validar.esVacio(txtEmail.getText()) && !Validar.validarVacio(txtEmail.getText())){
               if(!Validar.validarEmail(txtEmail.getText())){
                   lblErrorEmail.setVisible(true);
                   txtEmail.requestFocus();
               }else{
                   lblErrorEmail.setVisible(false);
               txtComBsAs.requestFocus();
               }
           }else{
            lblErrorEmail.setVisible(false);   
            txtComBsAs.requestFocus();
           }
       }
    }//GEN-LAST:event_txtEmailKeyPressed

    private void txtCelularKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCelularKeyPressed
        if(evt.getKeyCode()==evt.VK_ENTER){
           validarSiCajaDeTextoEsVacia(txtCelular.getText(), txtCelular, lblErrorCelular, txtNextel,NO_ULTIMO);
       }
    }//GEN-LAST:event_txtCelularKeyPressed

    private void txtComBsAsKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtComBsAsKeyPressed
         if(evt.getKeyCode()==evt.VK_ENTER){
           validarSiCajaDeTextoEsVacia(txtComBsAs.getText(), txtComBsAs, lblErrorComBsAs, txtComInt,NO_ULTIMO);
       }
    }//GEN-LAST:event_txtComBsAsKeyPressed

    private void txtComIntKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtComIntKeyPressed
         if(evt.getKeyCode()==KeyEvent.VK_ENTER){
           validarSiCajaDeTextoEsVacia(txtComInt.getText(), txtComInt, lblErrorComInt, txtComDepo,NO_ULTIMO);
       }
    }//GEN-LAST:event_txtComIntKeyPressed

    private void btnRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistrarActionPerformed
        if(esValidoElFormulario()){
            registrarCliente();
        }
    }//GEN-LAST:event_btnRegistrarActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
                frmCliente.btnActualizar.doClick();
    }//GEN-LAST:event_formWindowClosed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void txtNextelKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNextelKeyPressed
       if(evt.getKeyCode() == evt.VK_ENTER){
           txtEmail.requestFocus();
       }
    }//GEN-LAST:event_txtNextelKeyPressed

    private void txtClienteKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtClienteKeyPressed
        if(evt.getKeyCode()==evt.VK_ENTER){
           validarSiCajaDeTextoEsVacia(txtCliente.getText(), txtCliente, lblErrorCliente, txtTelefono,NO_ULTIMO);
           txtCliente.setText(txtCliente.getText().toUpperCase());
       }    
    }//GEN-LAST:event_txtClienteKeyPressed

    private void txtComDepoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtComDepoKeyPressed
        if(evt.getKeyCode()== KeyEvent.VK_ENTER){
            validarSiCajaDeTextoEsVacia(txtComDepo.getText(),txtComDepo,lblErrorComDepo,null,ULTIMO);
        }
    }//GEN-LAST:event_txtComDepoKeyPressed

    
    public void registrarCliente(){
            Cliente cliente = new Cliente();
            cliente.setNombre(txtCliente.getText());
            cliente.setTelefono(txtTelefono.getText());
            cliente.setDireccion(txtDireccion.getText());
            cliente.setCelular(txtCelular.getText());
            cliente.setNextel(txtNextel.getText());
            cliente.setEmail(txtEmail.getText());
            cliente.setComisionBa(BigDecimal.valueOf(Double.valueOf(txtComBsAs.getText())));
            cliente.setComisionInterior(BigDecimal.valueOf(Double.valueOf(txtComInt.getText())));
            cliente.setComisionDeposito(BigDecimal.valueOf(Double.valueOf(txtComDepo.getText())));
            if(chkComisionRechazo.isSelected()){
                cliente.setComisionRechazo("Y");
            } else {
                cliente.setComisionRechazo("N"); 
            }
            cliente.setEstado(1);
            
            String mensaje = clienteServices.insert(cliente);
        
            if(mensaje != null){
                    JOptionPane.showMessageDialog(this, mensaje, UIMessages.getInfoMessage("onCompleteInsert_title"), JOptionPane.INFORMATION_MESSAGE);
                    int resp = JOptionPane.showConfirmDialog(this, UIMessages.getQuestionMessage("register_another_cliente"), UIMessages.getQuestionMessage("register_another_title"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                    if (resp == 0){
                        limpiarFormulario();
                        }else{
                        dispose();
                        }
                                }else {
                                    JOptionPane.showMessageDialog(this, UIMessages.getErrorMessage("onInsertError"), UIMessages.getErrorMessage("defaultErrorTitle"), JOptionPane.ERROR_MESSAGE);
                                }
    
    }
    
    
    
    public boolean esValidoElFormulario(){
          for(JTextField txtField : txtList){  
              if(Validar.esVacio(txtField.getText()) || Validar.validarVacio(txtField.getText())){
                JOptionPane.showMessageDialog(this, UIMessages.getErrorMessage("requiredField") , UIMessages.getErrorMessage("requiredField_title"), JOptionPane.ERROR_MESSAGE);
                txtField.requestFocus();
                return false;
                }
          }     
          return true;       
    }
    
    
    private void valoresPorDefecto(){
        lblErrorCliente.setVisible(false);
        lblErrorTelefono.setVisible(false);
        lblErrorDireccion.setVisible(false);
        lblErrorCelular.setVisible(false);
        lblErrorEmail.setVisible(false);
        lblErrorComBsAs.setVisible(false);
        lblErrorComInt.setVisible(false);   
        lblErrorComDepo.setVisible(false);
        //chkComisionRechazo.setVisible(false);
    }
  
    
    private void cargarListaDeJTextFieldObligatorios(){
        txtList.add(txtCliente);
        txtList.add(txtTelefono);
        txtList.add(txtDireccion);
        txtList.add(txtCelular);
        txtList.add(txtComBsAs);
        txtList.add(txtComInt);
        txtList.add(txtComDepo);
    }
    

    public void limpiarFormulario(){
        txtCliente.setText("");
        txtDireccion.setText("");
        txtCelular.setText("");
        txtNextel.setText("");
        txtEmail.setText("");
        txtTelefono.setText("");
        txtComBsAs.setText("");
        txtComInt.setText("");     
    }
    
   
    private void validarSiCajaDeTextoEsVacia(String texto,JTextField cajita,JLabel labelDeError,JTextField siguiente_focus,int ultimo){
         if(!Validar.esVacio(texto) && !Validar.validarVacio(texto)){
                    if(ultimo == 0){   
                        siguiente_focus.requestFocus();
                                    }
                        labelDeError.setVisible(false);
            }  
            else
                {
                        cajita.requestFocus();
                        labelDeError.setVisible(true);
                 }
    }
    
    
    public static void main(String args[]) {
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                modalPanelAddCliente dialog = new modalPanelAddCliente(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JCheckBox chkComisionRechazo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel lblErrorCelular;
    private javax.swing.JLabel lblErrorCliente;
    private javax.swing.JLabel lblErrorComBsAs;
    private javax.swing.JLabel lblErrorComDepo;
    private javax.swing.JLabel lblErrorComInt;
    private javax.swing.JLabel lblErrorDireccion;
    private javax.swing.JLabel lblErrorEmail;
    private javax.swing.JLabel lblErrorTelefono;
    private javax.swing.JPanel panelContenedor;
    private javax.swing.JTextField txtCelular;
    private javax.swing.JTextField txtCliente;
    private javax.swing.JTextField txtComBsAs;
    private javax.swing.JTextField txtComDepo;
    private javax.swing.JTextField txtComInt;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtNextel;
    private javax.swing.JTextField txtTelefono;
    // End of variables declaration//GEN-END:variables
}
